<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="工夹具序列号" prop="seqId">
      <el-input v-model="dataForm.seqId" placeholder="工夹具序列号"></el-input>
    </el-form-item>
    <el-form-item label="工夹具代码" prop="code">
      <el-input v-model="dataForm.code" placeholder="工夹具代码"></el-input>
    </el-form-item>
    <el-form-item label="申请人id" prop="submitId">
      <el-input v-model="dataForm.submitId" placeholder="申请人id"></el-input>
    </el-form-item>
    <el-form-item label="寿命计数" prop="usedCount">
      <el-input v-model="dataForm.usedCount" placeholder="寿命计数"></el-input>
    </el-form-item>
    <el-form-item label="报废原因" prop="scrapReason">
      <el-input v-model="dataForm.scrapReason" placeholder="报废原因"></el-input>
    </el-form-item>
    <el-form-item label="照片" prop="scrapPhotoUrl">
      <el-input v-model="dataForm.scrapPhotoUrl" placeholder="照片"></el-input>
    </el-form-item>
    <el-form-item label="初审人id" prop="firstAcceptor">
      <el-input v-model="dataForm.firstAcceptor" placeholder="初审人id"></el-input>
    </el-form-item>
    <el-form-item label="初审时间" prop="firstTime">
      <el-input v-model="dataForm.firstTime" placeholder="初审时间"></el-input>
    </el-form-item>
    <el-form-item label="初审不通过原因" prop="firstReason">
      <el-input v-model="dataForm.firstReason" placeholder="初审不通过原因"></el-input>
    </el-form-item>
    <el-form-item label="终审人id" prop="finalAcceptor">
      <el-input v-model="dataForm.finalAcceptor" placeholder="终审人id"></el-input>
    </el-form-item>
    <el-form-item label="终审时间" prop="finalTime">
      <el-input v-model="dataForm.finalTime" placeholder="终审时间"></el-input>
    </el-form-item>
    <el-form-item label="终审不通过原因" prop="finalReason">
      <el-input v-model="dataForm.finalReason" placeholder="终审不通过原因"></el-input>
    </el-form-item>
    <el-form-item label="状态，0初审未通过 1 初审通过 2 终审未通过 3 终审通过" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态，0初审未通过 1 初审通过 2 终审未通过 3 终审通过"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          seqId: '',
          code: '',
          submitId: '',
          usedCount: '',
          scrapReason: '',
          scrapPhotoUrl: '',
          firstAcceptor: '',
          firstTime: '',
          firstReason: '',
          finalAcceptor: '',
          finalTime: '',
          finalReason: '',
          status: ''
        },
        dataRule: {
          seqId: [
            { required: true, message: '工夹具序列号不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '工夹具代码不能为空', trigger: 'blur' }
          ],
          submitId: [
            { required: true, message: '申请人id不能为空', trigger: 'blur' }
          ],
          usedCount: [
            { required: true, message: '寿命计数不能为空', trigger: 'blur' }
          ],
          scrapReason: [
            { required: true, message: '报废原因不能为空', trigger: 'blur' }
          ],
          scrapPhotoUrl: [
            { required: true, message: '照片不能为空', trigger: 'blur' }
          ],
          firstAcceptor: [
            { required: true, message: '初审人id不能为空', trigger: 'blur' }
          ],
          firstTime: [
            { required: true, message: '初审时间不能为空', trigger: 'blur' }
          ],
          firstReason: [
            { required: true, message: '初审不通过原因不能为空', trigger: 'blur' }
          ],
          finalAcceptor: [
            { required: true, message: '终审人id不能为空', trigger: 'blur' }
          ],
          finalTime: [
            { required: true, message: '终审时间不能为空', trigger: 'blur' }
          ],
          finalReason: [
            { required: true, message: '终审不通过原因不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态，0初审未通过 1 初审通过 2 终审未通过 3 终审通过不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `http://localhost:10000/gongjiajumain/scrapsubmithistory/info/${this.dataForm.id}`,
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.seqId = data.scrapSubmitHistory.seqId
                this.dataForm.code = data.scrapSubmitHistory.code
                this.dataForm.submitId = data.scrapSubmitHistory.submitId
                this.dataForm.usedCount = data.scrapSubmitHistory.usedCount
                this.dataForm.scrapReason = data.scrapSubmitHistory.scrapReason
                this.dataForm.scrapPhotoUrl = data.scrapSubmitHistory.scrapPhotoUrl
                this.dataForm.firstAcceptor = data.scrapSubmitHistory.firstAcceptor
                this.dataForm.firstTime = data.scrapSubmitHistory.firstTime
                this.dataForm.firstReason = data.scrapSubmitHistory.firstReason
                this.dataForm.finalAcceptor = data.scrapSubmitHistory.finalAcceptor
                this.dataForm.finalTime = data.scrapSubmitHistory.finalTime
                this.dataForm.finalReason = data.scrapSubmitHistory.finalReason
                this.dataForm.status = data.scrapSubmitHistory.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/gongjiajumain/scrapsubmithistory/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'seqId': this.dataForm.seqId,
                'code': this.dataForm.code,
                'submitId': this.dataForm.submitId,
                'usedCount': this.dataForm.usedCount,
                'scrapReason': this.dataForm.scrapReason,
                'scrapPhotoUrl': this.dataForm.scrapPhotoUrl,
                'firstAcceptor': this.dataForm.firstAcceptor,
                'firstTime': this.dataForm.firstTime,
                'firstReason': this.dataForm.firstReason,
                'finalAcceptor': this.dataForm.finalAcceptor,
                'finalTime': this.dataForm.finalTime,
                'finalReason': this.dataForm.finalReason,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
